""" Http 请求函数封装类 """

import requests
import urllib3

from util.logger import AutomationLogger


class RestAPIBase():
    def __init__(self, base_url, disable_ssl_verify=True, timeout=90):
        # Getting global logger
        self.log_client = AutomationLogger()
        self.logger = self.log_client.logger

        self.client = requests.session()
        self.timeout = timeout
        self.logger.debug(f'TIMEOUT={self.timeout}')
        self.logger.debug(f'Setting base_url: {base_url}')
        self.base_url = base_url
        self.logger.debug(f'Setting disable_ssl_verify: {disable_ssl_verify}')
        self.disable_ssl_verify = disable_ssl_verify
        if self.disable_ssl_verify:
            self.logger.debug('Disabling SSL Verify, will attempt to suppress insecure SSL warnings')
            urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

    def Get(self, url, headers=None, data=None, json=None, params=None, **kwargs):

        if headers is None:
            headers = {}

        self.logger.info(f'GET url: {url} headers: {headers} data: {data} json: {json} params: {params}')

        if self.disable_ssl_verify:
            response = self.client.get(url, headers=headers, data=data, json=json, params=params, verify=False,
                                       timeout=self.timeout, **kwargs)
        else:
            response = self.client.get(url, headers=headers, data=data, json=json, params=params, timeout=self.timeout,
                                       **kwargs)
        self.logger.info(f'Response {response.status_code}: {response.text}')

        return response

    def Post(self, url, headers=None, data=None, json=None, params=None, **kwargs):

        if headers is None:
            headers = {}

        self.logger.info(f'POST url: {url} headers: {headers} data: {data} json: {json} params: {params}')

        if self.disable_ssl_verify:
            response = self.client.post(url=url, headers=headers, data=data, json=json, params=params, verify=False,
                                        timeout=self.timeout, **kwargs)
        else:
            response = self.client.post(url=url, headers=headers, data=data, json=json, params=params,
                                        timeout=self.timeout, **kwargs)

        self.logger.info(f'Response {response.status_code}:{response.text}')

        return response

    def Delete(self, url, headers=None, data=None, json=None, params=None, **kwargs):

        if headers is None:
            headers = {}

        self.logger.info(f'DELETE url: {url} headers: {headers} data: {data} json: {json} params: {params}')

        if self.disable_ssl_verify:
            response = self.client.delete(url, headers=headers, data=data, json=json, params=params, verify=False,
                                          timeout=self.timeout, **kwargs)
        else:
            response = self.client.delete(url, headers=headers, data=data, json=json, params=params,
                                          timeout=self.timeout, **kwargs)

        self.logger.info(f'Response {response.status_code}:{response.text}')
        return response
